# -*- coding: utf-8 -*-
# @Time    : 2019/10/15 20:26
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : with_test.py


# 上下文管理器协议

class Sample(object):

    def __enter__(self):
        print("enter")  # 进入with时候调用
        # 获取资源
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        # 释放资源
        print("exit")  # 跳出with后自动调用

    def do_something(self):
        print("doing something")


with Sample() as sample:
    sample.do_something()
